package com.ruoyi.jwxt.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jwxt.domain.DTO.JwxtLabDTO;
import com.ruoyi.jwxt.domain.PO.JwxtLab;
import com.ruoyi.jwxt.service.IJwxtLabService;

/**
 * 实验室列表Controller
 */
@RestController
@RequestMapping("/jwxt/labs")
public class JwxtLabController extends BaseController
{
    @Autowired
    private IJwxtLabService jwxtLabsService;

    /**
     * 查询实验室列表列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:list')")
    @GetMapping("/list")
    public TableDataInfo list(JwxtLabDTO jwxtLabDTO )
    {
        startPage();
        List<JwxtLab> listPO=jwxtLabsService.select(jwxtLabsService.convert(jwxtLabDTO));
        TableDataInfo dataInfo=getDataTable(listPO);
        TableDataInfo tableDataCopy=new TableDataInfo();
        BeanUtils.copyProperties(dataInfo,tableDataCopy,"row");
        List<JwxtLabDTO> listDTO=listPO.stream().map(jwxtLabsService::convert).collect(Collectors.toList());
        tableDataCopy.setRows(listDTO);

        return tableDataCopy;
    }

    // /**
    //  * 导出实验室列表列表
    //  */
    // @PreAuthorize("@ss.hasPermi('jwxt:labs:export')")
    // @Log(title = "实验室列表", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, JwxtLabDTO jwxtLabDTO )
    // {
    //     List<JwxtLab> list = jwxtLabsService.select(jwxtLabs);
    //     ExcelUtil<JwxtLab> util = new ExcelUtil<JwxtLab>(JwxtLab.class);
    //     util.exportExcel(response, list, "实验室列表数据");
    // }

    /**
     * 获取实验室列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(
                jwxtLabsService.convert(jwxtLabsService.select(id)));
    }

    /**
     * 新增实验室列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:add')")
    @Log(title = "实验室列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JwxtLabDTO jwxtLabDTO )
    {
        return toAjax(jwxtLabsService.insert(jwxtLabsService.convert( jwxtLabDTO)));
    }

    /**
     * 修改实验室列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:edit')")
    @Log(title = "实验室列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JwxtLabDTO jwxtLabDTO )
    {
        return toAjax(jwxtLabsService.update(
                jwxtLabsService.convert(jwxtLabDTO)));
    }

    /**
     * 删除实验室列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:remove')")
    @Log(title = "实验室列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jwxtLabsService.delete(ids));
    }

    /**
     * 获得所有实验室列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:labs:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        return AjaxResult.success(jwxtLabsService.select());
    }
}
